﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;

namespace WasaWasa
{
    partial class MainWindow
    {
        Timer tmrMain;

        partial void InitializeTimer()
        {
            tmrMain = new Timer()
            {
                Interval = 100,
                AutoReset = true,
            };
            tmrMain.Elapsed += new ElapsedEventHandler(tmrMain_Elapsed);
            tmrMain.Start();
        }

        void tmrMain_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (tmrMain.Interval < 60000)
                tmrMain.Interval = 60000;
            try
            {
                foreach (var tlb in timelines)
                {
                    int interval = new Func<int>(() =>
                    {
                        switch (tlb.Type)
                        {
                            case TimelineType.Friends:
                                return def.FriendsTimelineInterval;
                            case TimelineType.Recent:
                                return def.RecentTimelineInterval;
                            case TimelineType.Public:
                                return def.PublicTimelineInterval;
                            case TimelineType.SL:
                                return def.SLTimelineInterval;
                            case TimelineType.Channel:
                                return def.ChannelTimelineInterval;
                            case TimelineType.User:
                                return def.UserTimelineInterval;
                            default:
                                return 0;
                        }
                    })();
                    if (0 < interval && tlb.RemainedTime-- == 0)
                    {
                        Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
                            new Action<TimelineBundle>(LoadTimeline), tlb);
                        System.Threading.Thread.Sleep(500);
                        tlb.RemainedTime = interval;
                    }
                }
                foreach (var ulb in userLists)
                {
                    int interval = new Func<int>(() =>
                    {
                        switch (ulb.Type)
                        {
                            case UserListType.Friends:
                                return def.FriendsListInterval;
                            case UserListType.Followers:
                                return def.FollowerListInterval;
                            case UserListType.Footprints:
                                return def.FootprintsListInterval;
                            default:
                                return 0;
                        }
                    })();
                    if (0 < interval && ulb.RemainedTime-- == 0)
                    {
                        Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
                            new Action<UserListBundle>(LoadUserList), ulb);
                        System.Threading.Thread.Sleep(500);
                        ulb.RemainedTime = interval;
                    }
                }
            }
            catch (InvalidOperationException)
            {
                tmrMain.Interval = 100;
            }
        }
    }
}
